package com.ruoyi.framework.config;

import com.github.pagehelper.Page;
import com.github.pagehelper.dialect.helper.MySqlDialect;
import org.apache.ibatis.cache.CacheKey;

import java.util.regex.Pattern;

/**
 * @author LengChen
 * @version 1.0
 * @date 2022-03-29
 */
public class MySqlDialectCustom extends MySqlDialect {

    @Override
    public String getPageSql(String sql, Page page, CacheKey pageKey) {
        Pattern compileFixed = Pattern.compile("/\\*limit\\*/");
        String compileLimit = "/\\*limit\\*/";
        if (compileFixed.matcher(sql).find()) {
            if (page.getStartRow() == 0) {
                sql = sql.replaceFirst(compileLimit, " LIMIT ? ");
            } else {
                sql = sql.replaceFirst(compileLimit, " LIMIT ?, ? ");
            }
            return sql;
        }
        return super.getPageSql(sql, page, pageKey);
    }
}